﻿using FangChan.Common;
using McsWeb.Common;
using McsWeb.Models;
using McsWeb.Models.ZBModels;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace McsWeb.Controllers
{
    public class TickerController : BaseController
    {
        // GET: Ticker
        public ActionResult Index()
        {
            return View();
        }

        public string IndexAsync(string CurrencyName, int pageIndex, int pageSize)
        {
            List<MarketModel> market = new List<MarketModel>();
              
            int total = Convert.ToInt32(FangChan.DBUtility.DbHelperSQL.GetSingle("select dbo.Fn_Value_Ticker('" + CurrencyName + "')"));
            DataTable dt = FangChan.DBUtility.DbHelperSQL.Query("select * from dbo.Fn_Table_Ticker('" + CurrencyName + "'," + pageIndex + "," + pageSize + ")").Tables[0];

            foreach (DataRow item in dt.Rows)
            {
                MarketModel m = new MarketModel();
                m.ID = Convert.ToInt32(item["ID"]);
                m.MarketName = Convert.ToString(item["MarketName"]);
                m.CurrencyName = Convert.ToString(item["CurrencyName"]);
                m.Last = Convert.ToDouble(item["Last"]);
                m.CreateTime = Convert.ToString(item["CreateTime"]);
                market.Add(m);
            }

            GridData<MarketModel> gridData = total > 0 ? new GridData<MarketModel>() { total = total, rows = market } : new GridData<MarketModel>() { total = total, rows = new List<MarketModel>() };

            return gridData.ToJson();
        }

        public ActionResult InsertAsync(string coin)
        {
            coin = coin + "_QC";
            //所有AJAX的结果，返回统一数据格式
            var result = new AjaxResult();

            MarketsListModel mlm = Tools.GetTickerModel(coin);

            MarketModel mm = new MarketModel();
            mm.MarketName = mlm.name;
            mm.CurrencyName = "eth";
            mm.Last = mlm.last;
            mm.CreateTime = DateTime.Now.ToString("yyyy-MM-dd");


            if (Convert.ToInt32(FangChan.DBUtility.DbHelperSQL.GetSingle("select count(*) from Ticker where CreateTime='" + DateTime.Now.ToString("yyyy-MM-dd") + "'")) == 0)
            {
                string sql = "insert into Ticker values('" + mm.MarketName + "','" + mm.CurrencyName + "'," + mm.Last + ",'" + mm.CreateTime + "')";

                int count = Convert.ToInt32(FangChan.DBUtility.DbHelperSQL.ExecuteSql(sql));

                if (count > 0)
                {
                    result.Type = AjaxResultType1.Success;
                    result.Content = "获取成功";
                }
                else
                {
                    result.Type = AjaxResultType1.Error;
                    result.Content = "获取失败";
                }
                return JsonEx(result);
            }
            else
            {
                result.Type = AjaxResultType1.Error;
                result.Content = "已有当天行情";
                return JsonEx(result);
            } 
        }
    }
}